package com.example.admin.brainwave.net;

import android.app.Activity;
import android.text.TextUtils;

import com.example.admin.brainwave.MyApp;
import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.bean.TestDataBean;
import com.example.admin.brainwave.bean.TestRecordBean;
import com.example.admin.brainwave.dao.DataDB1;
import com.example.admin.brainwave.dao.DataDB2;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.ITaskCallbackListener;
import com.example.admin.brainwave.utils.JsonUtils;
import com.example.admin.brainwave.utils.OkHttpUtil;
import com.example.admin.brainwave.utils.ProgressDialogManager;
import com.example.admin.brainwave.utils.RxDataTool;
import com.example.admin.brainwave.utils.RxTask;
import com.example.admin.brainwave.utils.SPHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class SaveTestDataTask extends RxTask<String, Integer, CurrencyBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public SaveTestDataTask(Activity activity,
                            ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected CurrencyBean doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.post(Const.SAVE_RECORD_DETAIL, getParam());
        if (!TextUtils.isEmpty(result)) {
            CurrencyBean currencyBean = JsonUtils.parseObject(result, CurrencyBean.class);
            return currencyBean;
        }
        return null;
    }

    @Override
    protected void onPostExecute(CurrencyBean result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private Map<String, Object> getParam() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> map = new HashMap<>();

        TestRecordBean testRecordBean = new TestRecordBean();
        float num = new Random().nextFloat();
        testRecordBean.setScore(num);
        SPHelper.getInstance(activity).putString("num", num + "");
        testRecordBean.setTesteeId(SPHelper.getInstance(activity).getString("userId", ""));
        testRecordBean.setTestTime(df.format(new Date()));


        TestDataBean tD1 = new TestDataBean();
        tD1.setRound(1);
        List<Double> listRound1Fp1 = new ArrayList<>();
        List<Double> listRound1Fp2 = new ArrayList<>();
        List<Double> listRound1Fp3 = new ArrayList<>();
        List<DataDB1> round1Data = MyApp.getDaoSession().getDataDB1Dao().loadAll();
        for (int i = 0; i < round1Data.size(); i++) {
            listRound1Fp1.add(round1Data.get(i).getFp1Data());
            listRound1Fp2.add(round1Data.get(i).getFp2Data());
            listRound1Fp3.add(round1Data.get(i).getFp3Data());
        }
        tD1.setFp1Data(RxDataTool.listToString(listRound1Fp1, ';'));
        tD1.setFp2Data(RxDataTool.listToString(listRound1Fp2, ';'));
        tD1.setFp3Data(RxDataTool.listToString(listRound1Fp3, ';'));


        TestDataBean tD2 = new TestDataBean();
        tD2.setRound(2);
        List<Double> listRound2Fp1 = new ArrayList<>();
        List<Double> listRound2Fp2 = new ArrayList<>();
        List<Double> listRound2Fp3 = new ArrayList<>();
        List<DataDB2> round2Data = MyApp.getDaoSession().getDataDB2Dao().loadAll();
        for (int i = 0; i < round2Data.size(); i++) {
            listRound2Fp1.add(round2Data.get(i).getFp1Data());
            listRound2Fp2.add(round2Data.get(i).getFp2Data());
            listRound2Fp3.add(round2Data.get(i).getFp3Data());
        }
        tD2.setFp1Data(RxDataTool.listToString(listRound2Fp1, ';'));
        tD2.setFp2Data(RxDataTool.listToString(listRound2Fp2, ';'));
        tD2.setFp3Data(RxDataTool.listToString(listRound2Fp3, ';'));


        map.put("testRecord", testRecordBean);
        map.put("testRecordData1", tD1);
        map.put("testRecordData2", tD2);
        return map;
    }
}